<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="/WEB-INF/jsp/includeTop.jsp" %>
<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>

<form id="editFunctionTemplateForm">
<input type="hidden" name="updatekey" value="${functionTemplate.id}">

<table width="100%" border="0" cellpadding="0" cellspacing="5">
  <tr>
    <td valign="top">
      <table class="dbborder" width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="bottom" class="blueTableBg">&nbsp;函数模板管理&nbsp;-&nbsp;编辑模板</td>
        </tr>
        <tr>
          <td>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td>
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td class="dashTopLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashTop" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashTopRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td align="center">
                        <table width="100%" border="0" cellpadding="0" cellspacing="3">
                          <tr>
                            <td width="114" height="20" class="globalText">函数名称</td>
                            <td height="20" class="globalText">
                            	<input id="functionName" type="text" class="txtbox" size="20" name="functionName" value="${functionTemplate.functionName}"/>
                            </td>
                          </tr>
                          <tr>
                            <td width="114" height="20" class="globalText">函数模板描述</td>
                            <td height="20" class="globalText">
                            	<input id="description" type="text" class="txtbox" size="20" name="description" value="${functionTemplate.description}" />
                            </td>
                          </tr>
                          <tr>
                            <td width="114" height="20" class="globalText">函数类型</td>
                            <td height="20" class="globalText">
                            	<select name="functionTypeSlt">
                            	  <option value="0" <c:if test="${functionTemplate.functionType == '0'}">selected</c:if>>数据函数</option>
                            	  <option value="1" <c:if test="${functionTemplate.functionType == '1'}">selected</c:if>>系统函数</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="sysFunctionTR">
                            <td width="114" height="20" class="globalText">函数内容</td>
                            <td height="20" class="globalText">
                            	<select name="sysFunctionSlt">
                            	  <option value="0">日期</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="relationTableTR">
                            <td width="114" height="20" class="globalText">对应表</td>
                            <td height="20" class="globalText">
                            	<select name="relationTableSlt">
                            	  <option value="dorder" <c:if test="${functionTemplate.relationTable == 'dorder'}">selected</c:if>>运单表</option>
                            	  <option value="bdepartment" <c:if test="${functionTemplate.relationTable == 'bdepartment'}">selected</c:if>>组织结构表</option>
                            	  <option value="dclient" <c:if test="${functionTemplate.relationTable == 'dclient'}">selected</c:if>>客户表</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="relationFieldTR">
                            <td width="114" height="20" class="globalText">对应字段</td>
                            <td height="20" class="globalText">
                            	<select name="relationFieldSlt">
	                            	<c:if test="${functionTemplate.relationTable == 'dorder' || functionTemplate.functionType == '1'}"><!-- 用于初始状态为系统函数，然后切换至数据函数时，能够出现初始的下拉框 -->
	                            	  <option value="orderid" <c:if test="${functionTemplate.relationField == 'orderid'}">selected</c:if>>运单编号</option>
	                            	  <option value="cargono" <c:if test="${functionTemplate.relationField == 'cargono'}">selected</c:if>>货号</option>
	                            	  <option value="cargoname" <c:if test="${functionTemplate.relationField == 'cargoname'}">selected</c:if>>货物名称</option>
	                            	  <option value="cargocount" <c:if test="${functionTemplate.relationField == 'cargocount'}">selected</c:if>>件数</option>
	                            	  <option value="clientid" <c:if test="${functionTemplate.relationField == 'clientid'}">selected</c:if>>客户编号</option>
	                            	  <option value="shipper" <c:if test="${functionTemplate.relationField == 'shipper'}">selected</c:if>>发货人</option>
	                            	  <option value="shipperphone" <c:if test="${functionTemplate.relationField == 'shipperphone'}">selected</c:if>>发货人电话</option>
	                            	  <option value="shipperMP" <c:if test="${functionTemplate.relationField == 'shipperMP'}">selected</c:if>>发货人手机</option>
	                            	  <option value="signaturer" <c:if test="${functionTemplate.relationField == 'signaturer'}">selected</c:if>>收货人</option>
	                            	  <option value="signaturephone" <c:if test="${functionTemplate.relationField == 'signaturephone'}">selected</c:if>>收货人电话</option>
	                            	  <option value="signatureMP" <c:if test="${functionTemplate.relationField == 'signatureMP'}">selected</c:if>>收货人手机</option>
	                            	  <option value="totalfee" <c:if test="${functionTemplate.relationField == 'totalfee'}">selected</c:if>>总运费</option>
	                            	  <option value="sendfee" <c:if test="${functionTemplate.relationField == 'sendfee'}">selected</c:if>>已付费</option>
	                            	  <option value="arrivefee" <c:if test="${functionTemplate.relationField == 'arrivefee'}">selected</c:if>>提付费</option>
	                            	  <option value="monthfee" <c:if test="${functionTemplate.relationField == 'monthfee'}">selected</c:if>>月结费</option>
	                            	  <option value="gatherfee" <c:if test="${functionTemplate.relationField == 'gatherfee'}">selected</c:if>>代收款</option>
	                            	  <option value="loaddeptid" <c:if test="${functionTemplate.relationField == 'loaddeptid'}">selected</c:if>>起运地公司编号</option>
	                            	  <option value="unloadeptid" <c:if test="${functionTemplate.relationField == 'unloadeptid'}">selected</c:if>>目的地公司编号</option>
	                            	  <option value="sendcargotype3" <c:if test="${functionTemplate.relationField == 'sendcargotype3'}">selected</c:if>>送货方式(1-自提，2-送货)</option>
	                            	</c:if>
	                            	<c:if test="${functionTemplate.relationTable == 'bdepartment'}">
	                            			<c:choose>
	                            			  <c:when test="${functionTemplate.relationField == 'loaddept.deptcode'}">
																			  <option value="loaddept.deptcode" <c:if test="${functionTemplate.relationField == 'loaddept.deptcode'}">selected</c:if>>公司编号</option>
																			  <option value="loaddept.deptname" <c:if test="${functionTemplate.relationField == 'loaddept.deptname'}">selected</c:if>>公司名称</option>
																			  <option value="loaddept.simplename" <c:if test="${functionTemplate.relationField == 'loaddept.simplename'}">selected</c:if>>公司简称</option>
																			  <option value="loaddept.deptadd" <c:if test="${functionTemplate.relationField == 'loaddept.deptadd'}">selected</c:if>>公司地址</option>
																			  <option value="loaddept.sendphone" <c:if test="${functionTemplate.relationField == 'loaddept.sendphone'}">selected</c:if>>发货电话</option>
																			  <option value="loaddept.arrivequeryphone" <c:if test="${functionTemplate.relationField == 'loaddept.arrivequeryphone'}">selected</c:if>>到货电话</option>
																			  <option value="loaddept.servicephone" <c:if test="${functionTemplate.relationField == 'loaddept.servicephone'}">selected</c:if>>服务电话</option>
																			  <option value="loaddept.city" <c:if test="${functionTemplate.relationField == 'loaddept.city'}">selected</c:if>>所在城市</option>
	                            			  </c:when>
	                            			  <c:otherwise>
																			  <option value="unloaddept.deptcode" <c:if test="${functionTemplate.relationField == 'unloaddept.deptcode'}">selected</c:if>>公司编号</option>
																			  <option value="unloaddept.deptname" <c:if test="${functionTemplate.relationField == 'unloaddept.deptname'}">selected</c:if>>公司名称</option>
																			  <option value="unloaddept.simplename" <c:if test="${functionTemplate.relationField == 'unloaddept.simplename'}">selected</c:if>>公司简称</option>
																			  <option value="unloaddept.deptadd" <c:if test="${functionTemplate.relationField == 'unloaddept.deptadd'}">selected</c:if>>公司地址</option>
																			  <option value="unloaddept.sendphone" <c:if test="${functionTemplate.relationField == 'unloaddept.sendphone'}">selected</c:if>>发货电话</option>
																			  <option value="unloaddept.arrivequeryphone" <c:if test="${functionTemplate.relationField == 'unloaddept.arrivequeryphone'}">selected</c:if>>到货电话</option>
																			  <option value="unloaddept.servicephone" <c:if test="${functionTemplate.relationField == 'unloaddept.servicephone'}">selected</c:if>>服务电话</option>
																			  <option value="unloaddept.city" <c:if test="${functionTemplate.relationField == 'unloaddept.city'}">selected</c:if>>所在城市</option>
	                            			  </c:otherwise>
	                            			</c:choose>
	                            	</c:if>
	                            	<c:if test="${functionTemplate.relationTable == 'dclient'}">
																  <option value="clientid" <c:if test="${functionTemplate.relationField == 'clientid'}">selected</c:if>>客户编号</option>
																  <option value="clientname" <c:if test="${functionTemplate.relationField == 'clientname'}">selected</c:if>>客户名称</option>
																  <option value="simpleclientname" <c:if test="${functionTemplate.relationField == 'simpleclientname'}">selected</c:if>>客户简称</option>
																  <option value="SPphone" <c:if test="${functionTemplate.relationField == 'SPphone'}">selected</c:if>>手机号</option>
	                            	</c:if>
                            	</select>
                            </td>
                          </tr>
                          <tr id="associatedFieldTR">
                            <td width="114" height="20" class="globalText">关联字段</td>
                            <td height="20" class="globalText">
                            	<select name="associatedFieldSlt">
                            		<c:choose>
                            		  <c:when test="${functionTemplate.relationTable == 'dorder'}">
                            		    <c:if test="${functionTemplate.relationField == 'clientid'}">
																		  <option value="clientid" <c:if test="${functionTemplate.associatedField == 'clientid'}">selected</c:if>>客户编号</option>
																		  <option value="clientname" <c:if test="${functionTemplate.associatedField == 'clientname'}">selected</c:if>>客户名称</option>
																		  <option value="simpleclientname" <c:if test="${functionTemplate.associatedField == 'simpleclientname'}">selected</c:if>>客户简称</option>
																		  <option value="SPphone" <c:if test="${functionTemplate.associatedField == 'SPphone'}">selected</c:if>>手机号</option>
                            		    </c:if>
                            		    <c:if test="${functionTemplate.relationField == 'loaddeptid'}">
																		  <option value="loaddept.deptcode" <c:if test="${functionTemplate.associatedField == 'loaddept.deptcode'}">selected</c:if>>公司编号</option>
																		  <option value="loaddept.deptname" <c:if test="${functionTemplate.associatedField == 'loaddept.deptname'}">selected</c:if>>公司名称</option>
																		  <option value="loaddept.simplename" <c:if test="${functionTemplate.associatedField == 'loaddept.simplename'}">selected</c:if>>公司简称</option>
																		  <option value="loaddept.deptadd" <c:if test="${functionTemplate.associatedField == 'loaddept.deptadd'}">selected</c:if>>公司地址</option>
																		  <option value="loaddept.sendphone" <c:if test="${functionTemplate.associatedField == 'loaddept.sendphone'}">selected</c:if>>发货电话</option>
																		  <option value="loaddept.arrivequeryphone" <c:if test="${functionTemplate.associatedField == 'loaddept.arrivequeryphone'}">selected</c:if>>到货电话</option>
																		  <option value="loaddept.servicephone" <c:if test="${functionTemplate.associatedField == 'loaddept.servicephone'}">selected</c:if>>服务电话</option>
																			<option value="loaddept.city" <c:if test="${functionTemplate.associatedField == 'loaddept.city'}">selected</c:if>>所在城市</option>
                            		    </c:if>
                            		    <c:if test="${functionTemplate.relationField == 'unloadeptid'}">
																		  <option value="unloaddept.deptcode" <c:if test="${functionTemplate.associatedField == 'unloaddept.deptcode'}">selected</c:if>>公司编号</option>
																		  <option value="unloaddept.deptname" <c:if test="${functionTemplate.associatedField == 'unloaddept.deptname'}">selected</c:if>>公司名称</option>
																		  <option value="unloaddept.simplename" <c:if test="${functionTemplate.associatedField == 'unloaddept.simplename'}">selected</c:if>>公司简称</option>
																		  <option value="unloaddept.deptadd" <c:if test="${functionTemplate.associatedField == 'unloaddept.deptadd'}">selected</c:if>>公司地址</option>
																		  <option value="unloaddept.sendphone" <c:if test="${functionTemplate.associatedField == 'unloaddept.sendphone'}">selected</c:if>>发货电话</option>
																		  <option value="unloaddept.arrivequeryphone" <c:if test="${functionTemplate.associatedField == 'unloaddept.arrivequeryphone'}">selected</c:if>>到货电话</option>
																		  <option value="unloaddept.servicephone" <c:if test="${functionTemplate.associatedField == 'unloaddept.servicephone'}">selected</c:if>>服务电话</option>
																			<option value="unloaddept.city" <c:if test="${functionTemplate.associatedField == 'unloaddept.city'}">selected</c:if>>所在城市</option>
                            		    </c:if>
                            		    <c:if test="${functionTemplate.relationField != 'clientid' || functionTemplate.relationField != 'loaddeptid' && functionTemplate.relationField != 'unloadeptid'}">
																			<option value="">----</option>
                            		    </c:if>
                            		  </c:when>
                            		  <c:otherwise><option value="">----</option></c:otherwise>
                            		</c:choose>
                            	</select>
                            </td>
                          </tr>
                          <tr>
                            <td align="center" colspan="2">
                              <table border="0" cellpadding="0" cellspacing="2">
                                <tr>
                                  <td><input type="button" name="saveBtn" class="btnStyle" value="  保存  "></td>
                                  <td><input type="button" name="cancelBtn" class="btnStyle" value="  取消  "></td>
                                </tr>
                              </table>
                            </td>
                          </tr>
                        </table>
                      </td>
                      <td class="dashRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashBottomLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashBottom" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashBottomRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>

<script type="text/javascript">
	$(document).ready(function() {
    var editFunctionTemplateForm = $("form[id=editFunctionTemplateForm]");
    var saveBtn = editFunctionTemplateForm.find(":button[name=saveBtn]");
    var cancelBtn = editFunctionTemplateForm.find(":button[name=cancelBtn]");

    saveBtn.click(function() {
    	document.getElementById("editFunctionTemplateForm").action="<c:url value='editFunctionTemplate.html' />";
    	document.getElementById("editFunctionTemplateForm").method="post";
    	
		  var functionName = document.getElementById("functionName").value.trim();
			if (validateEmpty(functionName)) {
				alert("函数名称 不能为空");
				return false;
			}

	   var description = document.getElementById("description").value.trim();
		 if (validateEmpty(description)) {
				alert("函数模板描述 不能为空");
				return false;
		 }
    	editFunctionTemplateForm.submit();
    });

    cancelBtn.click(function() {
    	document.getElementById("editFunctionTemplateForm").action = "<c:url value='functionTemplateMain.html' />";
    	document.getElementById("editFunctionTemplateForm").method="get";
    	editFunctionTemplateForm.submit();
    });
    
		//数据函数
    var functionTypeSlt = editFunctionTemplateForm.find("select[name=functionTypeSlt]");
    var relationTableSlt = editFunctionTemplateForm.find("select[name=relationTableSlt]");
    var relationFieldSlt = editFunctionTemplateForm.find("select[name=relationFieldSlt]");
 	  var associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");

 	  
    var relationTableTR = editFunctionTemplateForm.find("#relationTableTR");
    var relationFieldTR = editFunctionTemplateForm.find("#relationFieldTR");
 	  var	associatedFieldTR = editFunctionTemplateForm.find("#associatedFieldTR");
 	  
 	  var	sysFunctionTR = editFunctionTemplateForm.find("#sysFunctionTR");
 	 	//sysFunctionTR.hide();
 	 	var currentFunctionType = ${functionTemplate.functionType};
    renderFunctionTypeRelated(currentFunctionType); 	

    functionTypeSlt.change(function() {
      relationTableSlt = editFunctionTemplateForm.find("select[name=relationTableSlt]");
      relationFieldSlt = editFunctionTemplateForm.find("select[name=relationFieldSlt]");
 	  	associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");
      
    	var currentSltValue = $(this).val();
    	
    	renderFunctionTypeRelated(currentSltValue);
    });

    registerRelationFieldSltOrReloadAndReRegister();

    relationTableSlt.change(function() {
    	var currentSltValue = $(this).val();
    	
    	if (currentSltValue == "bdepartment" || currentSltValue == "dclient") {
	    	associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");
      	$.get("<c:url value='associatedFieldEmptySelect.html' />", 
      	  function(data){
      			associatedFieldSlt.replaceWith($(data).html());           		
        });
      }
        
    	if (currentSltValue == "dorder") {
      	$.get("<c:url value='relationFieldOrderSelect.html' />", 
      	  function(data){
        		relationFieldSlt.replaceWith($(data).html());
          			
        		registerRelationFieldSltOrReloadAndReRegister(); 		           		
        });
      } else if (currentSltValue == "bdepartment") {
        	$.get("<c:url value='relationFieldDepartmentSelect.html' />", 
 	      	  function(data){
        			relationFieldSlt.replaceWith($(data).html());
          			
        			registerRelationFieldSltOrReloadAndReRegister(); 		      			
 	        });
      } else if (currentSltValue == "dclient") {
      	$.get("<c:url value='relationFieldClientSelect.html' />", 
      	  function(data){
      			relationFieldSlt.replaceWith($(data).html());
      			
      			registerRelationFieldSltOrReloadAndReRegister(); 			
        });
      }

    });
    
    function registerRelationFieldSltOrReloadAndReRegister() {
  	  	relationFieldSlt = editFunctionTemplateForm.find("select[name=relationFieldSlt]");//Reload Slt
       	relationFieldSlt.change(function() {
	       	var currentSltValue = $(this).val();
	       	if (currentSltValue == "clientid") {//客户编号 -> 客户表
	       	  associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");

	   	     	$.get("<c:url value='associatedFieldClientSelect.html' />", 
	   	    	  function(data){
	   	     			associatedFieldSlt.replaceWith($(data).html());
	   	      	});
	         } else if (currentSltValue == "loaddeptid" || currentSltValue == "unloadeptid") {//起运地公司编号 or 目的地公司编号 -> 组织结构表
		       	  associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");
		       	  
							if (currentSltValue == "loaddeptid") {
		   	     		$.get("<c:url value='associatedFieldLoadDepartmentSelect.html' />", 
			  	    	    	  function(data){
			  	    	     			associatedFieldSlt.replaceWith($(data).html());
		  	    	      	});
			         		} else {
				   	     		$.get("<c:url value='associatedFieldUnloadDepartmentSelect.html' />", 
			   	    	    	  function(data){
			   	    	     			associatedFieldSlt.replaceWith($(data).html());
		   	    	      	});
			         }
         	 } else {
   	       	  associatedFieldSlt = editFunctionTemplateForm.find("select[name=associatedFieldSlt]");

			     		$.get("<c:url value='associatedFieldEmptySelect.html' />", 
			    	  function(data){
			     			associatedFieldSlt.replaceWith($(data).html());
			      	});
           }
       });
    }

    function renderFunctionTypeRelated(currentFunctionTypeValue) {
    	if (currentFunctionTypeValue == "0") {//数据函数
    		relationTableTR.show();
    		relationFieldTR.show();
    		associatedFieldTR.show();

      	sysFunctionTR.hide();
      } else {//系统函数
    	  relationTableTR.hide();
    	  relationFieldTR.hide();
    	  associatedFieldTR.hide();

    	  sysFunctionTR.show();
      }        
    }
	});
</script>